package com.szxy.single;

/**
 * @Author:zwer
 * @Date:2020/4/14 15:34
 * @Description:com.szxy.reflect
 * @Version:1.0
 * 单例模式
 **/
interface Single {

}

/**
 *  枚举式
 */
enum Single0{
    INSTANCE
}

/**
 * 饿汉式
 */
class Single1 {
    private static Single1 instance = new Single1();

    private Single1() {}

    public static Single1 getInstance() {
        return instance;
    }

}


/**
 * 懒汉式
 */
class Single2 {
    private static Single2 instance = null;

    private Single2() {}

    public static synchronized Single2 getInstance() {
        if(instance == null) {
            instance = new Single2();
        }
        return instance;
    }
}

/**
 * 懒汉式优化
 */
class Single3 {
    private static Single3 instance = null;

    private Single3() {}

    public static Single3 getInstance() {
        if (instance == null) {
            synchronized (Single3.class) {
                if (instance == null) {
                    instance = new Single3();
                }
            }
        }
        return instance;
    }
}

/**
 * 内部类
 */
class Single4{

    private Single4() {}

    public static Single4 getInstance() {
        return Inner.instance;
    }

    private static class Inner {
        private static Single4 instance = new Single4();
    }

}



